//
//  HSYQRViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/20.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYQRViewController.h"
#import "ALAssetsLibrary+hsyLibrary.h"
#import "ALAssetsLibrary+CustomPhotoAlbum.h"
#import "UIImage+Tools.h"
#import "UIImage+QR.h"
#import "HSYQrHelpViewController.h"

@interface HSYQRViewController ()

@property (nonatomic, weak) IBOutlet UIImageView *qrImageView;
@property (nonatomic, weak) IBOutlet UIActivityIndicatorView *activityView;

@property (nonatomic, weak) IBOutlet UILabel *qrCodeLabel;
@property (nonatomic, weak) IBOutlet UILabel *shopNameLabel;

@end

@implementation HSYQRViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor hsyDefaultBackgroudColor];
    
    self.qrCodeLabel.text = [NSString stringWithFormat:@"二维码编号：%@", self.qrModel.code];
    self.shopNameLabel.text = [NSString stringWithFormat:@"所属门店：%@", self.qrModel.shopName];
    
//    [self.activityView startAnimating];
    
    
    self.qrImageView.image = [self qrImage];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}

- (void)setupNavigationBarBackgroundImage{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(1, 1)]  forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];

    [self.leftAreaLBtn setImage:[UIImage imageNamed:@"left"]  forState:UIControlStateNormal];
    [self.titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.titleButton setTitle:@"店铺二维码" forState:UIControlStateNormal];
    
    
    [self.rightAreaRBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.rightArea.mas_right);
        make.width.mas_equalTo(@50);
        make.centerY.mas_equalTo(self.rightArea.mas_centerY);
        make.height.mas_equalTo(@30);
    }];

    [self.rightAreaRBtn setImage:[UIImage imageNamed:@"qr_help"] forState:UIControlStateNormal];
}

- (void)handleNavigationAction:(HSYNavigationActionType)actionType{
    if (actionType == HSYNavActionLeftAreaLeft) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    if (actionType == HSYNavActionRightAreaRight) {
        [self pushToHelp];
    }
}

- (NSString *)qrString{
    if (!self.qrModel) return nil;

    return  [NSString stringWithFormat:@"%@?code=%@&sign=%@", self.qrModel.qrUrl, self.qrModel.code, self.qrModel.sign];

}

- (UIImage *)qrImage{
    if (!self.qrModel) return nil;
    
    NSString *qrString = [self qrString];
    return [UIImage qrImageFromString:qrString];
}

- (void)pushToHelp{
    HSYQrHelpViewController *vc = [[HSYQrHelpViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)save:(id)sender{
    
    if (!self.qrModel) {
        [self showMessage:@"获取二维码失败"];
        return;
    }
    
    UIImage *image = [UIImage cardImageWithQrImage:self.qrImageView.image qrCode:[self qrString]];
    
    [[ALAssetsLibrary defaultAssetsLibrary] saveImage:image toAlbum:@"钱包++" completion:^(NSURL *assetURL, NSError *error) {
        [self showMessage:@"保存成功"];
    } failure:^(NSError *error) {
        [self showMessage:@"保存失败"];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
